package com.health.ts.controller.main;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.dao.consultation.VQueryConsulationMapper;
import com.health.ts.dao.register.VRemoteListMapper;
import com.health.ts.dao.transfer.VTransferListMapper;
import com.health.ts.entity.consultation.VQueryConsulation;
import com.health.ts.entity.register.VRemoteList;
import com.health.ts.entity.sysmanage.Msg;
import com.health.ts.entity.sysmanage.SysMenu;
import com.health.ts.entity.sysmanage.SysRole;
import com.health.ts.entity.sysmanage.SysUser;
import com.health.ts.entity.transfer.VTransferList;
import com.health.ts.service.sysmanage.MsgService;
import com.health.ts.service.sysmanage.SysMenuService;
import com.health.ts.service.sysmanage.SysRoleService;
import com.health.ts.service.sysmanage.SysUserService;
import com.health.ts.utils.SessionUserUtil;

/**
 * <p>
 * 主页数据服务
 * </p>
 *
 * @author ${author}
 * @since 2017-09-06
 */
@RestController
@RequestMapping(value = "/api/main", produces = BaseController.JSON_UTF8)
public class MainController extends BaseController {

	@Autowired
	SysMenuService sysMenuService;

	@Autowired
	MsgService msgService;

	@Autowired
	SysUserService sysUserService;

	@Autowired
	SysRoleService sysRoleService;

	@Autowired
	VRemoteListMapper vremoteListMapper;

	@Autowired
	VTransferListMapper vtransferListMapper;

	@Autowired
	VQueryConsulationMapper vqueryConsulationMapper;
	
	@Autowired
	RestTemplate restTemplate;

	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public ApiResult mainInfo() {
		String roles = SessionUserUtil.getUserRoles();
		Map<String, Object> resData = new HashMap<>();
		if (roles.contains("role_qcbefore")) {// 待前质控 1 有 0 无
			resData.put("qcbefore", 1);
			QueryWrapper<VQueryConsulation> conwrapper = new QueryWrapper<VQueryConsulation>();
			conwrapper.eq("con_status", "20");
			conwrapper.eq("assign_hid", SessionUserUtil.getUserHosId());
			Long conCount = vqueryConsulationMapper.selectCount(conwrapper);
			resData.put("before", conCount);
		}
		if (roles.contains("role_qcafter")) {// 待后质控 1 有 0 无
			resData.put("qcafter", 1);
			QueryWrapper<VQueryConsulation> conwrapper = new QueryWrapper<VQueryConsulation>();
			conwrapper.eq("con_status", "50");
			conwrapper.eq("assign_hid", SessionUserUtil.getUserHosId());
			Long conCount = vqueryConsulationMapper.selectCount(conwrapper);
			resData.put("after", conCount);
		}
		if (roles.contains("role_assign")) {// 待分诊 1 有 0 无
			resData.put("assign", 1);
			QueryWrapper<VQueryConsulation> conwrapper = new QueryWrapper<VQueryConsulation>();
			conwrapper.eq("con_status", "30");
			conwrapper.eq("assign_hid", SessionUserUtil.getUserHosId());
			Long conCount = vqueryConsulationMapper.selectCount(conwrapper);
			resData.put("qcassign", conCount);
		}
		if (roles.contains("role_report")) {// 待报告 1 有 0 无
			resData.put("report", 1);
			QueryWrapper<VQueryConsulation> conwrapper = new QueryWrapper<VQueryConsulation>();
			conwrapper.eq("con_status", "40");
			conwrapper.eq("assign_hid", SessionUserUtil.getUserHosId());
			Long conCount = vqueryConsulationMapper.selectCount(conwrapper);
			resData.put("qcreport", conCount);
		}
		/******************** 用户 ***************************/
		QueryWrapper<SysUser> userwrapper = new QueryWrapper<SysUser>();
		userwrapper.orderByDesc("create_time");
		Long userCount = sysUserService.count(userwrapper);
		resData.put("admin", userCount);
		/******************** 角色 ***************************/
		QueryWrapper<SysRole> rolewrapper = new QueryWrapper<SysRole>();
		rolewrapper.orderByDesc("create_time");
		Long roleCount = sysRoleService.count(rolewrapper);
		resData.put("role", roleCount);
		/******************** 资源 ***************************/
		QueryWrapper<SysMenu> menuwrapper = new QueryWrapper<SysMenu>();
		menuwrapper.eq("menu_classify", "2");
		menuwrapper.eq("menu_type", "2");
		Long menuCount = sysMenuService.count(menuwrapper);
		resData.put("menu", menuCount);
		/******************** 门诊 ***************************/
		QueryWrapper<VRemoteList> remotewrapper = new QueryWrapper<VRemoteList>();
		remotewrapper.eq("isReport", "0");// 待报告
		remotewrapper.eq("target_docid", SessionUserUtil.getUserId());
		Long remoteCount = vremoteListMapper.selectCount(remotewrapper);
		resData.put("remote", remoteCount);
		/******************** 转诊 ***************************/
		QueryWrapper<VTransferList> wrapper = new QueryWrapper<VTransferList>();
		wrapper.eq("apply_docid", SessionUserUtil.getUserId());
		Long tranConut = vtransferListMapper.selectCount(wrapper);
		resData.put("tran", tranConut);
		QueryWrapper<VTransferList> wrapper1 = new QueryWrapper<VTransferList>();
		wrapper1.eq("assign_hid", SessionUserUtil.getUserHosId());// 只有目标医院的审核管理员可以审核的转诊
		wrapper1.eq("isDone", "0");
		wrapper1.eq("isAgree", "0");
		Long tranAgreeConut = vtransferListMapper.selectCount(wrapper1);
		resData.put("tranAgreeConut", tranAgreeConut);
		/******************** 消息 ***************************/
		QueryWrapper<Msg> msgwrapper = new QueryWrapper<Msg>();
		msgwrapper.orderByDesc("createTime");
		Long msgCount = msgService.count(msgwrapper);
		resData.put("msg", msgCount);
		return ApiResult.success(resData);
	}
}
